package com.tangye.massageshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tangye.massageshop.constants.SysConstant;
import com.tangye.massageshop.entity.CompanyEntity;
import com.tangye.massageshop.entity.UserEntity;
import com.tangye.massageshop.enums.CompanyStatusEnum;
import com.tangye.massageshop.enums.RoleEnum;
import com.tangye.massageshop.exception.ShopException;
import com.tangye.massageshop.repository.CompanyRepository;
import com.tangye.massageshop.service.CompanyService;
import com.tangye.massageshop.service.UserService;
import com.tangye.massageshop.mapper.CompanyMapper;
import com.tangye.massageshop.util.MD5Utils;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Log4j2
@Service
public class CompanyServiceImpl implements CompanyService {

    @Resource
    private CompanyMapper companyMapper;
    @Resource
    private CompanyRepository companyRepository;

    @Resource
    private UserService userService;
    @Override
    public List<CompanyEntity> getAll() {
        QueryWrapper<CompanyEntity> queryWrapper = new QueryWrapper();
        return companyMapper.selectList(queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public UserEntity addCompany(CompanyEntity companyEntity) {
        CompanyEntity companyEntityDb = companyRepository.selectEnableByName(companyEntity.getCompanyName());
        if (Objects.nonNull(companyEntityDb)) {
            throw new ShopException("公司已存在");
        }
        companyEntity.setCompanyStatus(CompanyStatusEnum.NORMAL.getCode());
        companyMapper.insert(companyEntity);

        UserEntity userEntity = new UserEntity();
        userEntity.setCompanyId(companyEntity.getCompanyId());
        userEntity.setCompanyName(companyEntity.getCompanyName());
        userEntity.setUserName(RandomStringUtils.randomAlphabetic(6));
        userEntity.setPassword(MD5Utils.encryptByMd5(SysConstant.INIT_PASSWORD));
        userEntity.setRole(RoleEnum.COMPANY.getCode());
        userService.add(userEntity);
        return userEntity;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateCompany(CompanyEntity companyEntity) {
        if (Objects.nonNull(companyEntity.getCompanyId())) {
            CompanyEntity companyEntityDb = companyMapper.selectById(companyEntity.getCompanyId());

            if (Objects.isNull(companyEntityDb)) {
                log.error("公司不存在");
                return;
            }
            CompanyEntity update = new CompanyEntity();
            update.setCompanyId(companyEntity.getCompanyId());
            update.setCompanyName(companyEntity.getCompanyName());
            update.setCompanyAddress(companyEntity.getCompanyAddress());
            update.setCompanyContacts(companyEntity.getCompanyContacts());
            update.setCompanyPhone(companyEntity.getCompanyPhone());
            companyMapper.updateById(update);

            if (!companyEntity.getCompanyName().equals(companyEntityDb.getCompanyName())) {
                log.info("开始更新用户中公司名");
                userService.updateCompanyName(companyEntity.getCompanyId(), companyEntity.getCompanyName());
                log.info("结束更新用户中公司名");

                log.info("开始更新店铺中公司名");

                log.info("结束更新店铺中公司名");
            }
        }
    }
}
